//
//  DetaisWebViewController.m
//  Alltelevisionnews
//
//  Created by dllo on 15/11/28.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "DetaisWebViewController.h"

@interface DetaisWebViewController ()<UIWebViewDelegate, WXApiDelegate>

@property(nonatomic, strong)UIView *wechatView;
@property(nonatomic, strong)UIWebView *webView;
@property(nonatomic, strong)UIView *nilView;

@end

@implementation DetaisWebViewController
// 声明一个私有的成员变量
{   // 进度轮
    UIActivityIndicatorView *acView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
//    NSLog(@"%@", self.strId);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fenxiang.png"] style:0 target:self action:@selector(clack:)];
    
    [self createData];
    // 指定进度轮的大小
    acView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32.0f, 32.0f)];
    // 设置样式
    acView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    // 设置进度轮的中心
    acView.center = self.view.center;
    [self.view addSubview:acView];
}

- (void)createData {
    
    // WebView
    self.webView = [[UIWebView alloc] init];
    self.webView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.webView];
    self.webView.userInteractionEnabled = YES;
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.width.equalTo(self.view);
    }];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
//    self.hud = [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
//    self.hud.labelText = @"正在加载";
    NSURL *url = [NSURL URLWithString:self.strId];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [self.webView loadRequest:request];

//    self.hud.hidden = YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
    
    // 进度轮开始
    [acView startAnimating];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // 进度轮结束
    [acView stopAnimating];
    //  加载完成时,去掉loading效果
    [acView removeFromSuperview];
}

///上面分享的点击方法
- (void)clack:(UIBarButtonItem *)button {
    
    self.wechatView = [[UIView alloc] init];
    self.wechatView.backgroundColor = [UIColor grayColor];
    [self.webView addSubview:self.wechatView];
    [self.wechatView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.equalTo(self.webView);
        make.bottom.equalTo(self.webView);
        make.height.mas_equalTo(60);
    }];
    
    self.nilView = [[UIView alloc] init];
    self.nilView.backgroundColor = [UIColor clearColor];
    [self.webView addSubview:self.nilView];
    [self.nilView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.webView);
        make.top.equalTo(self.webView);
        make.bottom.equalTo(self.wechatView.mas_top);
    }];
    ////给nilview加点击事件
    UITapGestureRecognizer* singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];
    //    //点击的次数
    singleRecognizer.numberOfTapsRequired = 1;
    [self.nilView addGestureRecognizer:singleRecognizer];
    /////微信图标
    UIButton *wechatButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.wechatView addSubview:wechatButton];
    [wechatButton setBackgroundImage:[UIImage imageNamed:@"weixin.png"] forState:UIControlStateNormal];
    [wechatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.wechatView).with.offset(10);
        make.top.equalTo(self.wechatView.mas_top).with.offset(5);
        make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width / 375 * 70 - 20);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 375 * 70 - 20);
    }];
    wechatButton.layer.cornerRadius = [UIScreen mainScreen].bounds.size.width / 375 * 25;
    wechatButton.layer.masksToBounds = YES;
    [wechatButton addTarget:self action:@selector(wechatAction:) forControlEvents:UIControlEventTouchUpInside];
    // Qq
    UIButton *qqButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.wechatView addSubview:qqButton];
    [qqButton setBackgroundImage:[UIImage imageNamed:@"qq.png"] forState:UIControlStateNormal];
    [qqButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wechatButton.mas_right).with.offset(20);
        make.top.equalTo(self.wechatView.mas_top).with.offset(5);
        make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width / 375 * 70 - 20);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 375 * 70 - 20);
    }];
    qqButton.layer.cornerRadius = [UIScreen mainScreen].bounds.size.width / 375 * 25;
    qqButton.layer.masksToBounds = YES;
    [qqButton addTarget:self action:@selector(qqAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 微博
    UIButton *SinaButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.wechatView addSubview:SinaButton];
    [SinaButton setBackgroundImage:[UIImage imageNamed:@"weibo.png"] forState:UIControlStateNormal];
    [SinaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(qqButton.mas_right).with.offset(20);
        make.top.equalTo(self.wechatView.mas_top).with.offset(5);
        make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width / 375 * 70 - 20);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 375 * 70 - 20);
    }];
    SinaButton.layer.cornerRadius = [UIScreen mainScreen].bounds.size.width / 375 * 25;
    SinaButton.layer.masksToBounds = YES;
    [SinaButton addTarget:self action:@selector(SinaAction:) forControlEvents:UIControlEventTouchUpInside];
}

///中间隐藏view的点击方法
- (void)SingleTap:(UITapGestureRecognizer *)tap {
    self.nilView.hidden = YES;
    self.wechatView.hidden = YES;
}

////微信点击
- (void)wechatAction:(UIButton *)button {
    // 分享图片
//    WXMediaMessage *message = [WXMediaMessage message];
//    [message setThumbImage:[UIImage imageNamed:@"zhanweitu.png"]];
//    
//    WXImageObject *ext = [WXImageObject object];
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"zhanweitu" ofType:@"png"];
//    ext.imageData = [NSData dataWithContentsOfFile:filePath];
//    UIImage* image = [UIImage imageWithData:ext.imageData];
//    ext.imageData = UIImagePNGRepresentation(image);
//    message.mediaObject = ext;
//    
//    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
//    req.bText = NO;
//    
//    req.message = message;
//    req.scene = WXSceneSession;
//    [WXApi sendReq:req];
//        req.scene = WXSceneTimeline;
//        [WXApi sendReq:req];
        UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"祝你观看愉快" message:@"请选择您要分享到微信的路径" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:aleryController animated:YES completion:nil];
        UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"好友列表" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
            req.text = [NSString stringWithFormat:@"我在三人行新闻网看到了一段好的视频:%@", self.strId];
            req.bText = YES;
            req.scene = WXSceneSession;
            [WXApi sendReq:req];
        }];
        UIAlertAction *leftActionOne = [UIAlertAction actionWithTitle:@"朋友圈" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
            req.text = [NSString stringWithFormat:@"我在三人行新闻网看到了一段好的视频:%@", self.strId];
            req.bText = YES;
            req.scene = WXSceneTimeline;
            [WXApi sendReq:req];
        }];
        UIAlertAction *leftActionTwo = [UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
            req.text = [NSString stringWithFormat:@"我在三人行新闻网看到了一段好的视频:%@", self.strId];
            req.bText = YES;
            req.scene = WXSceneFavorite;
            [WXApi sendReq:req];
        }];
    UIAlertAction *leftActionThree = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        [aleryController addAction:leftAction];
        [aleryController addAction:leftActionOne];
        [aleryController addAction:leftActionTwo];
    [aleryController addAction:leftActionThree];

}
////qq图片点击方法
- (void)qqAction:(UIButton *)button {
    UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"祝你观看愉快" message:@"确认要分享到QQ好友或者QQ空间么" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:aleryController animated:YES completion:nil];
    UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        QQApiTextObject *txtObj = [QQApiTextObject objectWithText:[NSString stringWithFormat:@"我在三人行新闻网看到了一段好的视频:%@", self.strId]];
        SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:txtObj];
        //将内容分享到qq
        QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    }];
    UIAlertAction *leftActionOne = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [aleryController addAction:leftAction];
    [aleryController addAction:leftActionOne];
}
// 微博点击方法
- (void)SinaAction:(UIButton *)button {
    UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"祝你观看愉快" message:@"确认要分享新浪微博么" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:aleryController animated:YES completion:nil];
    UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       
        WBMessageObject *message = [WBMessageObject message];
        message.text = [NSString stringWithFormat:@"我在三人行新闻网看到了一段好的视频:%@", self.strId];
        WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message];
        [WeiboSDK sendRequest:request];
    }];
    UIAlertAction *leftActionOne = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [aleryController addAction:leftAction];
    [aleryController addAction:leftActionOne];
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
